
// BettingDataService.java
package com.zz.springboot.processor.bag;

import com.zz.springboot.processor.DataProcessor;
import com.zz.springboot.processor.bag.strategy.BagStrategy;
import com.zz.springboot.processor.bag.strategy.BagStrategyFactory;
import com.zz.springboot.processor.bag.strategy.TypeIndexManager;
import com.zz.springboot.util.DataDealUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class BagDataProcessor implements DataProcessor {

    @Autowired
    private TypeIndexManager typeIndexManager;

    @Autowired
    private BagStrategyFactory strategyFactory;

    /**
     * 解析用户输入的数据
     * @param inputData 用户输入的数据，如"包鼠100"、"包单500"等
     * @return 号码与金额的对应关系列表
     */
    public List<String> process(String inputData) {
        //1.提取金额
        Float money = DataDealUtils.extractMoneyFromStr(inputData);
        //2.获得“关键字”对应的“索引”。如：<"绿双","colorOddsEvens">
        Map.Entry<String, String> typeEntry = typeIndexManager.findTypeByKeywords(inputData);
        if (typeEntry == null) {
            throw new IllegalArgumentException("无法识别的输入格式: " + inputData);
        }
        String type = typeEntry.getValue();
        //3.再通过“索引”获得对应的“策略”。如：<"colorOddsEvens",BagColorOddsEvensStrategy>
        BagStrategy strategy = strategyFactory.getStrategy(type);
        if (strategy == null) {
            throw new IllegalArgumentException("不支持的类型: " + type);
        }
        //4.使用策略处理数据
        return strategy.appendList(inputData, money);
    }

    /**
     * 解析多用户/多数据输入
     * @param inputList 多个用户输入
     * @return 所有解析结果
     */
    public List<String> processBatchData(List<String> inputList) {
        List<String> allData = new ArrayList<>();
        for (String input : inputList) {
            allData.addAll(process(input));
        }
        return allData;
    }
}
